package com.htfyun.factorytest.engineerui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.htfyun.factorytest.FTFloatWindowService;
import com.htfyun.factorytest.config.NVRomKeys;

import com.htfyun.factorytest.R;


public class PCBAMainActivity extends Activity {

    private final static String TAG = "MainActivity";

    private Context mContext;

    private final static int REQUEST_CODE_RECORD    = NVRomKeys.ID_MAIN_MIC;
    private final static int REQUEST_CODE_TP        = NVRomKeys.ID_FINGER_TP;
    private final static int REQUEST_CODE_ETP       = NVRomKeys.ID_ELECTRIC_TP;
    private final static int REQUEST_CODE_KEY_AND_LED = NVRomKeys.ID_KEY_AND_LED;
    private final static int REQUEST_CODE_WIFI      = NVRomKeys.ID_WIFI;
    private final static int REQUEST_CODE_BT        = NVRomKeys.ID_BLUETOOTH;


    private int testFailFlag = 0;
    private int testOkFlag = 0;

    private Button btnRecorder;
    private Button btnTP;
    private Button btnETP;
    private Button btnTestWifi;
    private Button btnTestBT;
    private Button btnTestKeyAndLed;

    private SparseArray<Button> btnMap = new SparseArray<>();

    private Intent serviceIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_pcba_main);

        mContext = this;
        serviceIntent = new Intent(this, FTFloatWindowService.class);

        TextView txtVersion = (TextView) findViewById(R.id.txtVersion);
        txtVersion.setText(getString(R.string.app_version, getAppVersion()));

        btnRecorder = (Button)findViewById(R.id.btnRecorder);
        btnRecorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, RecorderActivity.class);
                intent.putExtra("isPcba", true);
                startActivityForResult(intent, REQUEST_CODE_RECORD);
            }
        });

        btnTP = (Button)findViewById(R.id.btnTP);
        btnTP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TouchDrawActivity.class);
                intent.putExtra("etp", false);
                intent.putExtra("isPcba", true);
                startActivityForResult(intent, REQUEST_CODE_TP);
            }
        });

        btnETP = (Button)findViewById(R.id.btnETP);
        btnETP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TouchDrawActivity.class);
                intent.putExtra("etp", true);
                intent.putExtra("isPcba", true);
                startActivityForResult(intent, REQUEST_CODE_ETP);
            }
        });

        btnTestWifi = (Button) findViewById(R.id.btnTestWifi);
        btnTestWifi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);

                Intent intent = new Intent(mContext, WifiActivity.class);
                intent.putExtra("isPcba", true);

//                Intent intent = new Intent();
//                intent.setClassName("com.android.settings",
//                        "com.android.settings.wifi.WifiSetupActivity");

                startActivityForResult(intent, REQUEST_CODE_WIFI); //直接进入手机中的wifi网络设置界面
            }
        });

        btnTestBT = (Button) findViewById(R.id.btnTestBT);
        btnTestBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, BTActivity.class);
                intent.putExtra("isPcba", true);
                startActivityForResult(intent, REQUEST_CODE_BT);
            }
        });

        btnTestKeyAndLed = (Button) findViewById(R.id.btnTestKeyAndLed);
        btnTestKeyAndLed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, KeyAndLedActivity.class);
                intent.putExtra("isPcba", true);
                startActivityForResult(intent, REQUEST_CODE_KEY_AND_LED);
            }
        });


        btnMap.append(REQUEST_CODE_RECORD, btnRecorder);
        btnMap.append(REQUEST_CODE_TP, btnTP);
        btnMap.append(REQUEST_CODE_ETP, btnETP);
        btnMap.append(REQUEST_CODE_WIFI, btnTestWifi);
        btnMap.append(REQUEST_CODE_BT, btnTestBT);
        btnMap.append(REQUEST_CODE_KEY_AND_LED, btnTestKeyAndLed);


        int flag = getPCBATestFlag();
        for (int i = 0; i < btnMap.size(); i++) {

            int requestCode = btnMap.keyAt(i);

            Button btn = btnMap.get(requestCode);
            if (btn == null) {
                continue;
            }

            if ((flag & requestCode) != 0) {

                btn.setText(getNewTextForBtn(btn, getResources().getString(R.string.test_ok)));

                testOkFlag |= requestCode;
            }
        }

    }


    @Override
    protected void onPause() {
        super.onPause();

        startService(serviceIntent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e(TAG, "keycode = " + keyCode);
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.e(TAG, "onActivityResult requestCode = " + requestCode + ", resultCode = " + resultCode);

        int resultResId = (resultCode == RESULT_OK) ? R.string.test_ok : R.string.test_fail;
        String testResult = getResources().getString(resultResId);


        Button btn = btnMap.get(requestCode);
        if (btn != null) {

            btn.setText(getNewTextForBtn(btn, testResult));
        }

        if (resultCode == RESULT_OK) {

            testOkFlag |= requestCode;
            testFailFlag &= ~requestCode;

        } else {

            testOkFlag &= ~requestCode;
            testFailFlag |= requestCode;

        }


       setPCBATestFlag(testOkFlag);

    }

    private String getNewTextForBtn(Button btn , String test_status) {
        String text = btn.getText().toString();
        String arrow = getResources().getString(R.string.arrow);
        String[] rmStatus = text.split(arrow);
        if (rmStatus.length > 1) {
            text = rmStatus[0];
        }
        return (text + arrow + test_status);
    }


    private String getAppVersion() {

        try {
            String pkName = getPackageName();

            return getPackageManager().getPackageInfo(
                    pkName, 0).versionName;


        } catch (Exception e) {

            e.printStackTrace();
        }
        return "1.20";
    }



    private void setPCBATestFlag( int flag) {
        SharedPreferences userSettings = getSharedPreferences("pcbaTest", 0);


        SharedPreferences.Editor editor = userSettings.edit();

        editor.putInt("pcbaFlag", flag);

        editor.apply();
    }

    private int getPCBATestFlag() {
        SharedPreferences userSettings = getSharedPreferences("pcbaTest", 0);

        return userSettings.getInt("pcbaFlag", 0);
    }

}
